package com.myeasyexcel.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myeasyexcel.config. UserDtoListener;
import com.myeasyexcel.constant.Constants;
import com.myeasyexcel.mapper.UserMapper;
import com.myeasyexcel.pojo.DTO.UserDto;
import com.myeasyexcel.pojo.VO.UserVo;
import com.myeasyexcel.pojo.entity.User;
import com.myeasyexcel.service.UserService;
import com.myeasyexcel.utils.EasyExcelUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 作者
 * @create 2025-03-06 10:35
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private final UserImportService importService;
    public void importExcel(MultipartFile file, HttpServletResponse response) {
        UserDtoListener<UserDto> listener = new UserDtoListener<>(5000, importService);
        EasyExcelUtil.importExcel(response, listener, file, UserDto.class, "用户导入错误数据");
    }

    @Override
    public void export(HttpServletResponse response) {
        FileInputStream fis = null;
        ServletOutputStream sos = null;
        try {
            response.setCharacterEncoding(Constants.ENCODE_UTF8);
            response.setHeader(Constants.RESP_HEADER_RawMaterial_KEY, Constants.RESP_HEADER_DISPOSITION_VALUE
                    + URLEncoder.encode(Constants.RawMaterial_IMPORT_TEMPLATE_NAME, Constants.ENCODE_UTF8));
            ClassPathResource classPathResource = new ClassPathResource(Constants.RawMaterial_Management_TEMPLATE_FILE_PATH);
            fis = new FileInputStream(classPathResource.getFile());
            sos = response.getOutputStream();
            IOUtils.copy(fis, sos);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("template  download error！");
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                if (sos != null) {
                    sos.flush();
                    sos.close();
                }
            } catch (IOException e) {
                log.error("io close error！");
            }
        }
    }

    @Override
    public void exportExcel(String param, HttpServletResponse response, HttpServletRequest request) {
        //根据查询条件去数据库查询数据
        //对数据进行处理
        List<User> users = list();
        List<UserVo> userVos = BeanUtil.copyToList(users, UserVo.class);
        //List<UserVo> userVos = new ArrayList<>();
        EasyExcelUtil.exportExcel(response, userVos, UserVo.class, "用户信息表");
    }
}
